package com.ningju.procurement.core.testmanager.service;

import com.ningju.procurement.common.mybatis.pager.PageContext;
import com.ningju.procurement.common.service.BaseServiceImpl;
import com.ningju.procurement.core.common.bean.Company;
import com.ningju.procurement.core.common.bean.User;
import com.ningju.procurement.core.testmanager.bean.TestProject;
import com.ningju.procurement.core.testmanager.bean.TestProjectUser;
import com.ningju.procurement.core.testmanager.dao.TestProjectDao;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户评测项目
 */
@Service
public class TestProjectService extends BaseServiceImpl<TestProjectDao, TestProject> {

    @Autowired
    private TestProjectDao testProjectDao;

    @Autowired
    private TestProjectUserService testProjectUserService;

    /**
     * 获取所属企业的所有评测项目
     * @param company
     * @return
     */
    public List<TestProject> getTestProjectByCompany(Company company) {
        Assert.isTrue(null != company && null != company.getId());

        return testProjectDao.selectTestProjectByCompany(company, PageContext.getPage());
    }

    /**
     * 获取所属用户的所有评测项目
     * @param user
     * @return
     */
    public List<TestProject> getTestProjectByUser(User user) {
        Assert.isTrue(null != user && null != user.getId());

        return testProjectDao.selectTestProjectByUser(user, PageContext.getPage());
    }

    /**
     * 所得用户下面由企业分配的评测项目
     * @return
     */
    public List<TestProject> getCompanyTestProjectByUser(User user) {
        Assert.isTrue(null != user && null != user.getId());

        return testProjectDao.selectCompanyTestProjectByUser(user, PageContext.getPage());
    }

    /**
     * 添加测试用户到评测项目
     * @param user
     * @param testProject
     * @return
     */
    public TestProject addUserToTestProject(User user, TestProject testProject) {

        Assert.notNull(user);

        List<User> users = new ArrayList<>();
        users.add(user);

        return addUserListToTestProject(users, testProject);
    }

    /**
     * 批量添加测试用户到评测项目
     * @param userList
     * @param testProject
     * @return
     */
    public TestProject addUserListToTestProject(List<User> userList, TestProject testProject) {
        Assert.notNull(userList);
        Assert.notNull(testProject);

        if(CollectionUtils.isEmpty(userList)) {
            testProject.setUserList(userList);
        } else {
            testProject.getUserList().forEach((user) -> {
                userList.forEach((userTo) -> {
                    if(!userTo.getId().equals(user.getId())) {
                        testProject.getUserList().add(userTo);
                    }
                });
            });
        }

        return testProject;
    }

    @Transactional
    public boolean save(TestProject testProject) {
        Assert.notNull(testProject);

        //保存项目评测用户
        if(CollectionUtils.isNotEmpty(testProject.getUserList())) {
            testProject.getUserList().parallelStream().forEach(user -> {
                TestProjectUser testProjectUser = new TestProjectUser();
                testProjectUser.setUserId(user.getId());
                testProjectUser.setTestProjectId(testProject.getId());
                testProjectUserService.save(testProjectUser);
            });
        }

        return super.save(testProjectDao, testProject);
    }

    public boolean destory(TestProject testProject) {

        //TODO: 删除用户所有的答题信息

        return testProjectDao.deleteById(testProject.getId()) > 0;
    }

}
